package com.mangowang.module.main.ui;



import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseFragment;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.Recharge;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.mangowang.module.main.adapter.AccountOneAdapter;
import com.mangowang.module.main.adapter.AccountTwoAdapter;
import com.mangowang.module.main.bean.AccountOneBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2018/2/2 0002.
 */

public class AccountFragment extends BaseFragment {


    Unbinder unbinder; // bufferKnife绑定

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.recycle_two)
    RecyclerView recycle_two;
    @BindView(R2.id.tv_balance)
    TextView tv_balance;

    @BindView(R2.id.ll_account_bg)
    LinearLayout ll_account_bg;

    @BindView(R2.id.tv_time_money)
    TextView tv_time_money;

    private AccountOneAdapter mAdapter;
    private AccountTwoAdapter mAdapterTwo;
    private List<AccountOneBean> datas = new ArrayList<>();
    private List<Recharge.wallet_detail> datasTwo = new ArrayList<>();
    private String position;
    private int fragmentPosition;

    public static AccountFragment NewInstance(String instanceType,String money,int fragmentPosition) {
        Bundle bundle = new Bundle();
        bundle.putString("position", instanceType);
        bundle.putString("money", money);
        bundle.putInt("fragmentPosition", fragmentPosition);
        AccountFragment messageTypeFragment = new AccountFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.home_fragment_account;
    }


    @Override
    protected void initView() {

        position = getArguments().getString("position");
        fragmentPosition = getArguments().getInt("fragmentPosition");
        if (fragmentPosition % 2 == 0)
        {
            ll_account_bg.setBackgroundColor(Color.parseColor("#FF2E8DF8"));
        }
        else
        {
            ll_account_bg.setBackgroundColor(Color.parseColor("#FFFFA53E"));
        }
        initRecyclerView();
        loadData();
    }

    private void loadData() {
        Recharge.wallet_month.Builder builder =  Recharge.wallet_month.newBuilder();
        builder.setWalletType(position);
        NetworkManager.INSTANCE.post(Apis.wallet_month, builder.build().toByteArray(),new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Recharge.wallet_month data = Recharge.wallet_month.parseFrom(response);
                List<Recharge.wallet_detail> lists = data.getWaletsList();
                Log.e("lists",lists.toString());

                if (lists.size()<=0) return;

                // 找出最大值
                List<Double> input = new ArrayList<Double>();
                List<AccountOneBean> datas = new ArrayList<AccountOneBean>();

                for (Recharge.wallet_detail temp :  lists) {
                    input.add(Double.parseDouble(temp.getAmount()));
                }
                Double max =  Collections.max(input);
                if (max==0)
                {
                    for (Recharge.wallet_detail temp :  lists) {
                        AccountOneBean tempBean =  new AccountOneBean();
                        tempBean.setAmount(temp.getAmount());
                        tempBean.setMonth(temp.getMonth().substring(2));
                        tempBean.setMonthAll(temp.getMonth());
                        int progress = 10;
                        tempBean.setProgressOne(progress);
                        tempBean.setProgressTwo(0);
                        tempBean.setProgress(progress);
                        datas.add(tempBean);
                    }
                }else
                {
                    for (Recharge.wallet_detail temp :  lists) {
                        AccountOneBean tempBean =  new AccountOneBean();
                        tempBean.setAmount(temp.getAmount());
                        tempBean.setMonth(temp.getMonth().substring(2));
                        tempBean.setMonthAll(temp.getMonth());
                        Double  amount =  Double.parseDouble(temp.getAmount());
                        int progress = (int)(amount / max * 100);
                        tempBean.setProgressOne(progress);
                        tempBean.setProgressTwo(0);
                        tempBean.setProgress(progress);
                        datas.add(tempBean);
                    }
                }

                // 加载第一个数据
                AccountOneBean first= datas.get(0);
                first.setProgressOne(0);
                first.setProgressTwo(first.getProgress());
                first.setSelect(true);
                tv_time_money.setText(first.getMonthAll() + " 收益");
                tv_balance.setText("¥ " + first.getAmount());
                mAdapter.addData(datas);

                // 加载月份数据
                loadMonthData(position,first.getMonthAll());

            }

            @Override
            public void onError(int errorCode, String errorMessage) {
            }
        });
    }

    private void loadMonthData(String position, String monthAll) {
        Recharge.wallet_month.Builder builder = Recharge.wallet_month.newBuilder();
        builder.setMonth(monthAll);
        builder.setWalletType(position);
        NetworkManager.INSTANCE.post(Apis.wallet_detail,builder.build().toByteArray(),new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Recharge.wallet_month data = Recharge.wallet_month.parseFrom(response);
                List<Recharge.wallet_detail> lists = data.getWaletsList();
                Log.e("monthData",lists.toString());
                datasTwo = lists;
                mAdapterTwo.setNewInstance(lists);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
            }
        });
    }

    @Override
    protected void init() {

    }



    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        recycle_two.setLayoutManager(new LinearLayoutManager(mContext));

        recyclerView.setAdapter(mAdapter = new AccountOneAdapter(mContext, datas));
        recycle_two.setAdapter(mAdapterTwo = new AccountTwoAdapter(mContext, datasTwo));
//        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
//            WebViewActivity.startActivity(mContext, WebApis. + "?member_id=" + datas.get(position).getMemberId());
            for (AccountOneBean bean : datas) {
                bean.setProgressOne(bean.getProgress());
                bean.setProgressTwo(0);
                bean.setSelect(false);
            }
            datas.get(position).setProgressOne(0);
            datas.get(position).setSelect(true);
            datas.get(position).setProgressTwo(datas.get(position).getProgress());
            tv_time_money.setText( datas.get(position).getMonthAll() + " 收益 ");
            tv_balance.setText("¥ " + datas.get(position).getAmount());
            mAdapter.notifyDataSetChanged();
            loadMonthData(this.position,datas.get(position).getMonthAll());

        });
        mAdapterTwo.setOnItemClickListener((adapter, view, position) -> {
            Intent intent  = new Intent(mContext,ZhangDanActivity.class);
            intent.putExtra("walletType",this.position);
            intent.putExtra("detailType",datasTwo.get(position).getMonth());
            intent.setType(datasTwo.get(position).getTitle());
            startActivity(intent);
        });

    }
}
